package com.hangzhou.xyj.ui.activity;

import android.graphics.Color;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.hangzhou.xyj.R;
import com.hangzhou.xyj.net.handler.ResetPasswordHandler;
import com.hangzhou.xyj.net.handler.SmsShortCodeHandler;
import com.hangzhou.xyj.net.http.HttpLoader;
import com.hangzhou.xyj.net.request.ResetPasswordReq;
import com.hangzhou.xyj.net.request.SmsShortCodeReq;
import com.hangzhou.xyj.ui.activity.base.BaseActivity;
import com.hangzhou.xyj.utils.PublicUtil;
import com.hangzhou.xyj.utils.StringUtil;
import com.hangzhou.xyj.utils.ToastUtil;

public class RetrievePasswordActivity extends BaseActivity implements View.OnClickListener {
    private Button mBtnGetCode;//获取验证码 按钮
    private EditText mEtTel;//手机号
    private EditText mEtPwd;//密码
    private Button mBtnConfirm;//确定
    private EditText mBtnShortCode;//验证码

    @Override
    public void initTop() {
        enableNetErrorView(false);
        // TODO Auto-generated method stub

    }

    @Override
    public void initLayouts() {
        setContentView(R.layout.activity_retrieve_password);
    }

    @Override
    public void initViews() {
        mBtnGetCode = (Button) findViewById(R.id.btn_get_code);
        mEtTel = (EditText) findViewById(R.id.et_tel);
        mEtPwd = (EditText) findViewById(R.id.et_password);
        mBtnConfirm = (Button) findViewById(R.id.btn_confirm);
        mBtnShortCode = (EditText) findViewById(R.id.et_short_code);
    }

    @Override
    public void initDatas() {
    }

    @Override
    public void initListeners() {
        mBtnGetCode.setOnClickListener(this);
        mEtPwd.setOnClickListener(this);
        mBtnConfirm.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_get_code://点击获取验证码按钮
                if (!checkInputBeforeGetCode()) return;//校验不通过 返回
                String tel = mEtTel.getText().toString().trim();
                SmsShortCodeReq smsShortCodeReq = new SmsShortCodeReq(tel, SmsShortCodeReq.TYPE_RESET_PASSWORD);
                HttpLoader.getInstance().sendRequest(new SmsShortCodeHandler(smsShortCodeReq, new MyHttpResponseListener() {
                    @Override
                    public void onSuccess(Object model) {
                        super.onSuccess(model);
                        ToastUtil.show("验证码已发送，注意查收！");
                    }
                }, 0));
                //点击“获取验证码”之后
                mBtnGetCode.setEnabled(false);
                mBtnGetCode.setBackgroundResource(R.drawable.bg_gray_rect);
                mBtnGetCode.setTextColor(Color.parseColor("#636363"));
                //倒计时60秒
                new CountDownTimer(60000, 1000) {
                    @Override
                    public void onTick(long millisUntilFinished) {
                        mBtnGetCode.setText(millisUntilFinished / 1000 + "秒重新获取");
                    }

                    @Override
                    public void onFinish() {
                        mBtnGetCode.setEnabled(true);
                        mBtnGetCode.setTextColor(Color.parseColor("#ffffff"));
                        mBtnGetCode.setBackgroundResource(R.drawable.bg_round_rect);
                        mBtnGetCode.setText("获取验证码");
                    }
                }.start();
                break;
            case R.id.btn_confirm://确定按钮
                if (!checkInputBeforeConfirm()) return;
                String telephoneNumber = mEtTel.getText().toString().trim();
                String password = mEtPwd.getText().toString().trim();
                String shortCode = mBtnShortCode.getText().toString().trim();
                ResetPasswordReq resetPasswordReq = new ResetPasswordReq(telephoneNumber, password, shortCode);
                HttpLoader.getInstance().sendRequest(new ResetPasswordHandler(resetPasswordReq, new MyHttpResponseListener() {
                    @Override
                    public void onSuccess(Object model) {
                        super.onSuccess(model);
                        ToastUtil.show("恭喜，密码重置成功！");
                        closeActivity();
                    }
                }, 0));
                break;
        }
    }

    /**
     * @return
     * @Description 获取验证码之前，校验输入的手机号
     * @author yaohuix
     * @date 2016/2/29 11:31
     */
    private boolean checkInputBeforeGetCode() {
        String tel = mEtTel.getText().toString().trim();
        if (StringUtil.isEmpty(tel)) {
            ToastUtil.show("手机号为空，请输入手机号");
            return false;
        }
        if (!PublicUtil.isMobilePhone(tel)) {
            ToastUtil.show("输入的手机号非法，请重新输入");
            mEtTel.setText("");
            return false;
        }
        return true;
    }


    /**
     * @param
     * @return 点击“确定” 进行校验
     * @Description
     * @author yaohuix
     * @date 2016/2/29 11:38
     */
    private boolean checkInputBeforeConfirm() {
        String pwd = mEtPwd.getText().toString().trim();
        if (!checkInputBeforeGetCode()) {
            return false;
        }
        if (StringUtil.isEmpty(pwd)) {
            ToastUtil.show("密码为空，请重新输入密码");//TODO 密码还要进行合理性校验
            return false;
        }
        return true;
    }

}
